﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Fsb.Utility
{
    public class SecurityHelper
    {
        /// <summary>
        /// DES加密方法
        /// </summary>
        /// <param name="strPlain">明文</param>
        /// <param name="strDESKey">密钥</param>
        /// <param name="strDESIV">向量</param>
        /// <returns>密文</returns>
        public string DESEncrypt(string strPlain, string strDESKey, string strDESIV)
        {
            byte[] bytesCipher;

            //把密钥转换成字节数组
            byte[] bytesDESKey = ASCIIEncoding.ASCII.GetBytes(strDESKey);
            //把向量转换成字节数组
            byte[] bytesDESIV = ASCIIEncoding.ASCII.GetBytes(strDESIV);
            //声明1个新的DES对象
            DESCryptoServiceProvider desEncrypt = new DESCryptoServiceProvider();
            //开辟一块内存流
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                //把内存流对象包装成加密流对象
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, desEncrypt.CreateEncryptor(bytesDESKey, bytesDESIV), CryptoStreamMode.Write))
                {
                    //把加密流对象包装成写入流对象
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        //写入流对象写入明文
                        swEncrypt.WriteLine(strPlain);

                        //把内存流转换成字节数组，内存流现在已经是密文了
                        bytesCipher = msEncrypt.ToArray();
                    }
                }
            }
            //把密文字节数组转换为字符串，并返回
            return UnicodeEncoding.Unicode.GetString(bytesCipher);
        }

        /// <summary>
        /// DES解密方法
        /// </summary>
        /// <param name="strCipher">密文</param>
        /// <param name="strDESKey">密钥</param>
        /// <param name="strDESIV">向量</param>
        /// <returns>明文</returns>
        public string DESDecrypt(string strCipher, string strDESKey, string strDESIV)
        {
            string strPlainText = string.Empty;

            //把密钥转换成字节数组
            byte[] bytesDESKey = ASCIIEncoding.ASCII.GetBytes(strDESKey);
            //把向量转换成字节数组
            byte[] bytesDESIV = ASCIIEncoding.ASCII.GetBytes(strDESIV);
            //把密文转换成字节数组
            byte[] bytesCipher = UnicodeEncoding.Unicode.GetBytes(strCipher);
            //声明1个新的DES对象
            DESCryptoServiceProvider desDecrypt = new DESCryptoServiceProvider();
            //开辟一块内存流，并存放密文字节数组
            using (MemoryStream msDecrypt = new MemoryStream(bytesCipher))
            {
                //把内存流对象包装成解密流对象
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, desDecrypt.CreateDecryptor(bytesDESKey, bytesDESIV), CryptoStreamMode.Read))
                {
                    //把解密流对象包装成读出流对象
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {
                        //明文=读出流的读出内容
                        strPlainText = srDecrypt.ReadLine();
                    }
                }
            }
            //返回明文
            return strPlainText;
        }
    }
}